package com.lube;

import com.lube.codec.MessageDecoder;
import com.lube.codec.MessageEncoder;
import com.lube.handler.ServerHandler;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.timeout.IdleStateHandler;

/**
 * 服务端初始化
 * 
 * @author caizhiping
 * @date 2017年1月24日 下午2:05:38
 */
public class ServerInitializer extends ChannelInitializer<SocketChannel> {

	@Override
	protected void initChannel(SocketChannel ch) throws Exception {
		ChannelPipeline pipeline = ch.pipeline();
		
		ch.pipeline().addLast(new IdleStateHandler(20, 0, 0));

		// 日志处理器
		pipeline.addLast(new LoggingHandler(LogLevel.INFO));

		// 添加解码器
		pipeline.addLast(new MessageDecoder());

		// 添加编码器
		pipeline.addLast(new MessageEncoder());
		// 添加业务处理器
		pipeline.addLast(new ServerHandler());
	}

}
